package com.vehicle_information_management_system.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle_information_management_system.dto.PeopleCount;
import com.vehicle_information_management_system.pojo.UserInfo;
import com.vehicle_information_management_system.service.UserInfoService;
import com.vehicle_information_management_system.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author huawei
* @description 针对表【user_info】的数据库操作Service实现
* @createDate 2025-02-28 13:48:03
*/
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
    implements UserInfoService{

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public UserInfo userLogin(String username,String password) {
        return userInfoMapper.selectOne(new QueryWrapper<UserInfo>().
                eq("username",username).
                eq("password", MD5.create().digestHex(password)));
    }

    @Override
    public void addUser(UserInfo userInfo) {
        userInfo.setPassword(MD5.create().digestHex(userInfo.getPassword()));
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void delUser(Integer id) {
        userInfoMapper.deleteById(id);
    }

    @Override
    public void updateUser(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public IPage<UserInfo> getAllUser(Integer pageNum,Integer pageSize,String search) {
        Page<UserInfo> page = new Page<>(pageNum,pageSize);
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(search)){
            queryWrapper.like("username",search);
        }
        return userInfoMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<UserInfo> getUserInfo() {
        return userInfoMapper.selectList(null);
    }

    @Override
    public UserInfo getUserInfoById(Integer id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public List<PeopleCount> getPeopleCountByDepartment() {
        return userInfoMapper.getPeopleCountByDepartment();
    }

}




